﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Reflection;
using System.IO;

namespace OBNEPGGrabber
{
    static class FileFetcher
    {
        private static string m_strURL = null;
        private static string m_strFileName = null;
        private static DirectoryInfo m_dirInfo = null;
        private static IStatusUpdate m_IUpdate = null;

        // Yeah, I know, not threadsafe, blabla, but in this case there is only one
        // worker thread :-) 
        public static void Fetch(string strURL, DirectoryInfo dirInfo, string strFileName, IStatusUpdate IStatusUpdate)
        {
            m_strURL = strURL;
            m_strFileName = strFileName;
            m_dirInfo = dirInfo;
            m_IUpdate = IStatusUpdate;
            FetchFile();
        }

        // Just fetch the file
        private static void FetchFile()
        {
            try
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFile(m_strURL, m_dirInfo.FullName + "\\" + m_strFileName);
            }
            catch (Exception)
            {
                m_IUpdate.UpdateStatus(String.Format("Error fetching {0}", m_strURL), false);
            }
        }
    }
}
